package cn.ac.radi.userver.umodel.controller;

import cn.ac.radi.userver.umodel.constant.ResultCodeConstant;
import cn.ac.radi.userver.umodel.constant.ResultStateConstant;
import cn.ac.radi.userver.umodel.entity.*;
import cn.ac.radi.userver.umodel.model.BaseLineModel;
import cn.ac.radi.userver.umodel.model.CatsModel;
import cn.ac.radi.userver.umodel.model.EIPsModel;
import cn.ac.radi.userver.umodel.model.SuperGlueModel;
import cn.ac.radi.userver.umodel.model.MyModel;
import cn.ac.radi.userver.umodel.util.Util;
import cn.edu.tongji.tjmodel.TjModel;
import com.mathworks.toolbox.javabuilder.MWCharArray;
import com.mathworks.toolbox.javabuilder.MWException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/model/k2")
public class ModelController {

    @Autowired
    private Environment env;

    private Logger logger = LoggerFactory.getLogger(this.getClass());


    @PostMapping(value = "/cva")
    @ResponseBody
    public ResponseEntity<CvaResult> test_cva(@RequestBody CvaParams params){

        String input1 = params.getInput1();
        String input2 = params.getInput2();
        String output = params.getOutput();
        int returnNum = 4;
        boolean ret = true;

        TjModel tjModel = null;

        try {
            tjModel = new TjModel();
            Object[] objs = tjModel.CVA_uncertain(returnNum, input1, input2, output);
            if(objs.length==returnNum)
            {
                int length = objs.length;
                for(int i=0; i<length; i++){
                    Object obj = objs[i];
                    MWCharArray mwValue = (obj instanceof MWCharArray ? ((MWCharArray) obj) : null);
                    if(mwValue!=null) {
                        String path = mwValue.toString();
                        System.out.println(path);
                    }
                }
            }
        }
        catch (MWException e){
            e.printStackTrace();
        }
        finally {
            //处理模型运行结果
            CvaResult rr = new CvaResult();
            return new ResponseEntity<>(rr, HttpStatus.OK);
        }
    }

    @PostMapping(value = "/lstm")
    @ResponseBody
    public ResponseEntity<LstmResult> lstmFunction(@RequestBody LstmParams params) throws Exception {

        String input1 = params.getInput1();
        String input2 = params.getInput2();
        String output = params.getOutput();
        int returnNum = 4;
        boolean ret = true;

        TjModel tjModel = null;

        try {
            tjModel = new TjModel();
            Object[] objs = tjModel.LSTM(returnNum, input1, input2, output);
            if(objs.length == returnNum){
                int length = objs.length;
                for(int i=0; i<length; i++){
                    Object obj = objs[i];
                    MWCharArray mwValue = (obj instanceof MWCharArray ? ((MWCharArray) obj) : null);
                    if(mwValue!=null) {
                        String path = mwValue.toString();
                        System.out.println(path);
                    }
                }
            }
        }
        catch (MWException e){
            e.printStackTrace();
        }
        finally {
        }

        //处理模型运行结果
        LstmResult rr = new LstmResult();
        return new ResponseEntity<>(rr, HttpStatus.OK);
    }

    @PostMapping(value = "/tjreg")
    @ResponseBody
    public ResponseEntity<TjregResult> tjregFunction(@RequestBody TjregParams params) throws Exception {

        String input1 = params.getInput1();
        String input2 = params.getInput2();
        String output = params.getOutput();
        int returnNum = 1;
        boolean ret = true;

        TjModel tjModel = null;


        try {
            tjModel = new TjModel();
            Object[] objs = tjModel.Tjreg(returnNum, input1, input2, output);
            if(objs.length == returnNum){
                int length = objs.length;
                for(int i=0; i<length; i++){
                    Object obj = objs[i];
                    MWCharArray mwValue = (obj instanceof MWCharArray ? ((MWCharArray) obj) : null);
                    if(mwValue!=null) {
                        String path = mwValue.toString();
                        System.out.println(path);
                    }
                }
            }
        }
        catch (MWException e){
            e.printStackTrace();
        }
        finally {
        }

        //处理模型运行结果
        TjregResult rr = new TjregResult();
        return new ResponseEntity<>(rr, HttpStatus.OK);
    }


    @PostMapping(value = "/baseline")
    @ResponseBody
    public ResponseEntity<BaselineResult> baseline(@RequestBody BaselineParams params) {

        BaseLineModel model = new BaseLineModel(this.env, this.logger);

        String input = params.getInput();
        String output = params.getOutput();

        boolean ret = model.run(input, output);

        BaselineResult rr = null;
        if(ret){
            rr = buildResult();
            return new ResponseEntity<>(rr, HttpStatus.OK);
        }
        else{
            rr = new BaselineResult();
            return new ResponseEntity<>(rr, HttpStatus.OK);
        }
//        MyModel myModel = new MyModel();
//        String pyEnvPath = env.getProperty("userver.env.python_conda_env");
//        String pyCodeHome = env.getProperty("userver.model.K2.python_home");
//
//
//        String pyCommand = myModel.buildCommand(); //python a.py input1 input2 output1
//        String command = "cmd /C conda activate " + pyEnvPath + " & " + pyCommand;

//        try{
//            model.run()
//        }
//        catch (IOException e){
//            e.printStackTrace();
//        }
//        finally {
//            CvaResult rr = new CvaResult();
//            return new ResponseEntity<>(rr, HttpStatus.OK);
//        }
    }

    private BaselineResult buildResult()
    {
        BaselineResult br = new BaselineResult();

        //遍历output文件夹，获取所有的jpg图像的路径。
        //for{
        String rootPath = env.getProperty("userver.env.web_home");
        String imagePath = "D:/project/deployment/program/apache-tomcat-9.0.41/webapps/userverdata/k2/baseline/pred_jj_0_0517(0,0).jpg";
        //String webPath = "/userverdata/k2/baseline/pred_jj_0_0517(0,0).jpg";
        String webPath = rootPath.replace(imagePath, "");

        br.addImage(webPath);
        // }

        return br;
    }



    @PostMapping(value = "/cats")
    @ResponseBody
    public ResponseEntity<CatsResult> Cats(@RequestBody CatsParams params) {

        CatsModel model = new CatsModel(this.env, this.logger);

        String mode = params.getMode();
        String input = params.getInput();
        String output = params.getOutput();

        boolean ret = model.run(mode, input, output);

        CatsResult rr = null;
        if(ret){
            rr = buildCatsResult();
            return new ResponseEntity<>(rr, HttpStatus.OK);
        }
        else{
            rr = new CatsResult();
            return new ResponseEntity<>(rr, HttpStatus.OK);
        }
//        MyModel myModel = new MyModel();
//        String pyEnvPath = env.getProperty("userver.env.python_conda_env");
//        String pyCodeHome = env.getProperty("userver.model.K2.python_home");
//
//
//        String pyCommand = myModel.buildCommand(); //python a.py input1 input2 output1
//        String command = "cmd /C conda activate " + pyEnvPath + " & " + pyCommand;

//        try{
//            model.run()
//        }
//        catch (IOException e){
//            e.printStackTrace();
//        }
//        finally {
//            CvaResult rr = new CvaResult();
//            return new ResponseEntity<>(rr, HttpStatus.OK);
//        }
    }

    private CatsResult buildCatsResult(){
        CatsResult br = new CatsResult();

        //便利output文件夹，获取所有的jpg图像的路径。
        //for{
        String rootPath = env.getProperty("userver.env.web_home");
        //String imagePath = "D:/project/deployment/program/apache-tomcat-9.0.41/webapps/userverdata/k2/baseline/pred_jj_0_0517(0,0).jpg";
        //String webPath = "/userverdata/k2/baseline/pred_jj_0_0517(0,0).jpg";
        //String webPath = rootPath.replace(imagePath, "");

       // br.addImage(webPath);
        // }

        return br;
    }



    @PostMapping(value = "/eips")
    @ResponseBody
    public ResponseEntity<EIPsResult> EIPs(@RequestBody EIPsParams params) {

        EIPsModel model = new EIPsModel(this.env, this.logger);

        String input = params.getInput();
        String order = params.getOrder();
        String output = params.getOutput();

        boolean ret = model.run(input, order, output);

        EIPsResult rr = null;
        if(ret){
            rr = buildEIPsResult();
            return new ResponseEntity<>(rr, HttpStatus.OK);
        }
        else{
            rr = new EIPsResult();
            return new ResponseEntity<>(rr, HttpStatus.OK);
        }
//        MyModel myModel = new MyModel();
//        String pyEnvPath = env.getProperty("userver.env.python_conda_env");
//        String pyCodeHome = env.getProperty("userver.model.K2.python_home");
//
//
//        String pyCommand = myModel.buildCommand(); //python a.py input1 input2 output1
//        String command = "cmd /C conda activate " + pyEnvPath + " & " + pyCommand;

//        try{
//            model.run()
//        }
//        catch (IOException e){
//            e.printStackTrace();
//        }
//        finally {
//            CvaResult rr = new CvaResult();
//            return new ResponseEntity<>(rr, HttpStatus.OK);
//        }
    }

    private EIPsResult buildEIPsResult(){
        EIPsResult br = new EIPsResult();

        //便利output文件夹，获取所有的jpg图像的路径。
        //for{
        String rootPath = env.getProperty("userver.env.web_home");
        //String imagePath = "D:/project/deployment/program/apache-tomcat-9.0.41/webapps/userverdata/k2/baseline/pred_jj_0_0517(0,0).jpg";
        //String webPath = "/userverdata/k2/baseline/pred_jj_0_0517(0,0).jpg";
        //String webPath = rootPath.replace(imagePath, "");

        // br.addImage(webPath);
        // }

        return br;
    }


    @PostMapping(value = "/superglue")
    @ResponseBody
    public ResponseEntity<SuperglueResult> Superglues(@RequestBody SuperglueParams params) {

        SuperGlueModel model = new SuperGlueModel(this.env, this.logger);

        String input = params.getInput1();
        String input2 = params.getInput2();
        String output = params.getOutput();

        boolean ret = model.run(input, input2, output);

        SuperglueResult rr = null;
        if(ret){
            rr = buildSuperglueResult();
            return new ResponseEntity<>(rr, HttpStatus.OK);
        }
        else{
            rr = new SuperglueResult();
            return new ResponseEntity<>(rr, HttpStatus.OK);
        }
//        MyModel myModel = new MyModel();
//        String pyEnvPath = env.getProperty("userver.env.python_conda_env");
//        String pyCodeHome = env.getProperty("userver.model.K2.python_home");
//
//
//        String pyCommand = myModel.buildCommand(); //python a.py input1 input2 output1
//        String command = "cmd /C conda activate " + pyEnvPath + " & " + pyCommand;

//        try{
//            model.run()
//        }
//        catch (IOException e){
//            e.printStackTrace();
//        }
//        finally {
//            CvaResult rr = new CvaResult();
//            return new ResponseEntity<>(rr, HttpStatus.OK);
//        }
    }

    private SuperglueResult buildSuperglueResult(){
        SuperglueResult br = new SuperglueResult();

        //便利output文件夹，获取所有的jpg图像的路径。
        //for{
        String rootPath = env.getProperty("userver.env.web_home");
        //String imagePath = "D:/project/deployment/program/apache-tomcat-9.0.41/webapps/userverdata/k2/baseline/pred_jj_0_0517(0,0).jpg";
        //String webPath = "/userverdata/k2/baseline/pred_jj_0_0517(0,0).jpg";
        //String webPath = rootPath.replace(imagePath, "");

        // br.addImage(webPath);
        // }

        return br;
    }
}

